@echo off
chcp 65001
echo ===============================================
echo 若依校园防欺凌系统启动脚本
echo ===============================================

echo.
echo [1/4] 检查MySQL服务状态...
sc query MySQL80 | find "RUNNING" >nul
if %errorlevel% neq 0 (
    echo MySQL服务未运行，正在启动...
    net start MySQL80
    timeout /t 5 /nobreak >nul
) else (
    echo MySQL服务正在运行
)

echo.
echo [2/4] 创建数据库并导入数据...

REM 查找MySQL安装路径
set MYSQL_PATH=""
if exist "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe" (
    set MYSQL_PATH="C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe"
) else if exist "C:\Program Files (x86)\MySQL\MySQL Server 8.0\bin\mysql.exe" (
    set MYSQL_PATH="C:\Program Files (x86)\MySQL\MySQL Server 8.0\bin\mysql.exe"
) else if exist "C:\mysql\bin\mysql.exe" (
    set MYSQL_PATH="C:\mysql\bin\mysql.exe"
) else (
    echo 未找到MySQL，请手动创建数据库 ry-vue
    echo 然后导入以下SQL文件：
    echo - sql/ry_20250417.sql
    echo - sql/quartz.sql
    echo - sql/campus_alarm_system.sql
    pause
    goto start_app
)

echo 使用MySQL路径: %MYSQL_PATH%

REM 创建数据库
echo 正在创建数据库...
%MYSQL_PATH% -u root -pwjrwjrwjr666 -e "CREATE DATABASE IF NOT EXISTS \`ry-vue\` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"

REM 导入基础数据
echo 正在导入基础数据...
%MYSQL_PATH% -u root -pwjrwjrwjr666 ry-vue < sql/ry_20250417.sql

REM 导入定时任务数据
echo 正在导入定时任务数据...
%MYSQL_PATH% -u root -pwjrwjrwjr666 ry-vue < sql/quartz.sql

REM 导入校园系统数据
echo 正在导入校园系统数据...
%MYSQL_PATH% -u root -pwjrwjrwjr666 ry-vue < sql/campus_alarm_system.sql

:start_app
echo.
echo [3/4] 编译项目...
call mvn clean package -DskipTests=true

echo.
echo [4/4] 启动应用...
echo 应用将在端口 8089 启动
echo 启动完成后可访问: http://localhost:8089
echo 默认用户名: admin  密码: admin123
echo.

cd ruoyi-admin\target
set JAVA_OPTS=-Xms256m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m

echo 正在启动应用，请稍候...
java -jar %JAVA_OPTS% ruoyi-admin.jar

pause 